<template>
  <div class="app-container">
    <el-button type="primary" @click="handleAdd">新建角色</el-button>

    <el-table :data="roleList" style="width: 100%;margin-top:30px;">
      <el-table-column align="center" label="角色名称" width="220">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="角色标识" width="220">
        <template slot-scope="scope">
          {{ scope.row.slug }}
        </template>
      </el-table-column>
      <el-table-column align="header-center" label="描述">
        <template slot-scope="scope">
          {{ scope.row.description }}
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="small" @click="handleEdit(scope)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDelete(scope)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog :visible.sync="dialogVisible" :title="dialogType==='edit'?'编辑角色':'新建角色'">
      <el-form :model="role" label-width="80px" label-position="left">
        <el-form-item label="角色名称">
          <el-input v-model="role.name" placeholder="角色名称" />
        </el-form-item>
        <el-form-item label="角色标识">
          <el-input v-model="role.slug" placeholder="角色名称" />
        </el-form-item>
        <el-form-item label="拥有权限">
          <el-tree
            :key="new Date().valueOf()"
            ref="treeRef"
            :data="this.permissionList"
            show-checkbox
            node-key="value"
            :default-checked-keys="this.role.permissions"
            :props="defaultProps"
          />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="role.description" type="textarea" placeholder="描述" />
        </el-form-item>
      </el-form>
      <div style="text-align:right;">
        <el-button type="danger" @click="dialogVisible=false">取消</el-button>
        <el-button type="primary" @click="confirmRole">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { deepClone } from '@/utils'
import { getRoles, addRole, updateRole, deleteRole } from '@/api/role'
import { getPermissionsNode } from '@/api/permission'
import success from '@/utils/operation-message'
const defaultRole = {
  name: '',
  slug: '',
  permissions: [],
  description: ''
}

export default {
  data() {
    return {
      role: Object.assign({}, defaultRole),
      roleList: [],
      permissionList: [],
      dialogVisible: false,
      dialogType: 'new',
      checkStrictly: false,
      defaultProps: {
        children: 'children',
        label: 'label'
      }
    }
  },
  computed: {
    routesData() {
      return this.routes
    }
  },
  created() {
    this.getRoles()
    this.getPermissions()
  },
  methods: {
    async getRoles() {
      const res = await getRoles()
      this.roleList = res.data
    },
    async getPermissions() {
      const res = await getPermissionsNode()
      this.permissionList = res
    },
    handleAdd() {
      this.role = Object.assign({}, deleteRole)
      this.dialogType = 'new'
      this.dialogVisible = true
    },
    handleEdit(scope) {
      this.dialogType = 'edit'
      this.dialogVisible = true
      this.role = deepClone(scope.row)
    },
    dispathPermission(id) {
      this.$router.push({
        path: '/permission/dispatch',
        query: {
          roleId: id
        }
      })
    },
    handleDelete(scope) {
      this.$confirm('您确定要删除该角色吗?', '', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          await deleteRole(scope.row.id)
          this.getRoles()
          success()
        })
        .catch(err => {
          console.error(err)
        })
    },
    async confirmRole() {
      const isEdit = this.dialogType === 'edit'
      this.role.permissions = this.$refs.treeRef.getCheckedKeys()

      if (isEdit) {
        await updateRole(this.role, this.role.id)
      } else {
        await addRole(this.role)
      }
      this.getRoles()
      this.dialogVisible = false
      success()
    }
  }
}
</script>

<style lang="scss" scoped>
  .app-container {
    .roles-table {
      margin-top: 30px;
    }
    .role-tree {
      margin-bottom: 30px;
    }
  }
</style>
